package com.disk.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.disk.annotation.GlobalInterceptor;
import com.disk.annotation.VerifyParam;
import com.disk.entity.dto.SessionWebUserDto;
import com.disk.entity.dto.UploadResultDto;
import com.disk.entity.enums.FileCategoryEnums;
import com.disk.entity.enums.FileDelFlagEnums;
import com.disk.entity.po.FileInfo;
import com.disk.entity.query.FileInfoQuery;
import com.disk.entity.vo.FileInfoVO;
import com.disk.entity.vo.PaginationResultVO;
import com.disk.entity.vo.ResponseVO;
import com.disk.exception.BusinessException;
import com.disk.mapper.FileInfoMapper;
import com.disk.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 文件信息 前端控制器
 * </p>
 *
 * @author zsy
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/file")
public class FileInfoController extends CommonFileController {

    @Resource
    private FileInfoService fileInfoService;
    @Autowired
    private FileInfoMapper fileInfoMapper;

    //获取文件列表
    @RequestMapping("/loadDataList")
    @GlobalInterceptor
    public ResponseVO LoadDataList(HttpSession session, FileInfoQuery query, String fileCategory) {
        FileCategoryEnums fileCategoryEnums = FileCategoryEnums.getByCode(fileCategory);
        if (null != fileCategoryEnums) {
            query.setFileCategory(fileCategoryEnums.getCategory());
        }
        query.setUserId(getUserInfoFromSession(session).getUserId());
        query.setOrderBy("last_update_time desc");
        query.setDelFlag(FileDelFlagEnums.USING.getFlag());
        PaginationResultVO result = fileInfoService.findListByPage(query);
        return getSuccessResponseVO(convert2PaginationVO(result, FileInfoVO.class));

    }

    @RequestMapping("/uploadFile")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO uploadFile(HttpSession session,
                                 String fileId,
                                 MultipartFile file,
                                 @VerifyParam(required = true) String fileName,
                                 @VerifyParam(required = true) String filePid,
                                 @VerifyParam(required = true) String fileMd5,
                                 @VerifyParam(required = true) Integer chunkIndex,
                                 @VerifyParam(required = true) Integer chunks) {

        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        UploadResultDto uploadResultDto = fileInfoService.uploadFile(webUserDto, fileId, file, fileName, filePid, fileMd5, chunkIndex, chunks);

        return getSuccessResponseVO(uploadResultDto);
    }

    @RequestMapping("/getImage/{imageFolder}/{imageName}")
    public void getImage(HttpServletResponse response, @PathVariable(value = "imageFolder") String imageFolder, @PathVariable(value = "imageName") String imageName) {
        super.getImage(response, imageFolder, imageName);
    }

    @RequestMapping("/ts/getVideoInfo/{fileId}")
    public void getVideoInfo(HttpServletResponse response, HttpSession session, @PathVariable(value = "fileId") String fileId) {
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        super.getFile(response, fileId, webUserDto.getUserId());
    }


    @RequestMapping("/getFile/{fileId}")
    public void getFile(HttpServletResponse response, HttpSession session, @PathVariable(value = "fileId") String fileId) {
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        super.getFile(response, fileId, webUserDto.getUserId());
    }

    //TODO 改前端的这个接口
    @RequestMapping("/newFoloder")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO newFolder(HttpSession session,
                                @VerifyParam(required = true) String filePid,
                                @VerifyParam(required = true) String fileName) {
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        FileInfo fileInfo = fileInfoService.newFolder(webUserDto.getUserId(), filePid, fileName);
        return getSuccessResponseVO(fileInfo);
    }

    @RequestMapping("/getFolderInfo")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO getFolderInfo(HttpSession session,
                                    @VerifyParam(required = true) String path) {
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        return super.getFolderInfo(path, webUserDto.getUserId());
    }


    @RequestMapping("rename")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO rename(HttpSession session,
                             @VerifyParam(required = true) String fileId,
                             @VerifyParam(required = true) String fileName) {
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        FileInfo newFileInfo = fileInfoService.rename(webUserDto.getUserId(), fileId, fileName);
        if (null == newFileInfo) {
            throw new BusinessException("重命名失败");
        }
        System.out.println(newFileInfo);
        return getSuccessResponseVO(newFileInfo);
    }

}
